package com.yungu.swift.order.helper;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.passenger.model.dto.PassengerCouponDto;
import com.yungu.swift.assets.passenger.service.PassengerCouponService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.order.model.param.OrderEvaluateParam;
import com.yungu.swift.order.model.vo.ApiJoinValuationVo;
import com.yungu.swift.order.model.vo.OrderCostItemVo;
import com.yungu.swift.system.sys.model.dto.SysPointSiteDto;
import com.yungu.swift.system.sys.model.dto.SysWayCalculationDto;
import com.yungu.swift.system.sys.model.dto.SysWayNightFareDto;
import com.yungu.swift.system.sys.model.param.FenceRaisePriceParam;
import com.yungu.swift.system.sys.model.vo.FenceRaisePriceVo;
import com.yungu.swift.system.sys.service.SysPointSiteService;
import com.yungu.swift.system.sys.service.SysWayCalculationService;
import com.yungu.swift.system.sys.service.SysWayFenceService;
import com.yungu.swift.system.sys.service.SysWayNightFareService;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 跨城估价
 */
@Slf4j
@Component
public class CrosstownEvaluateHelper {

    private static SysWayFenceService sysWayFenceService;
    private static SysWayNightFareService sysWayNightFareService;
    private static SysWayCalculationService sysWayCalculationService;
    private static SysPointSiteService sysPointSiteService;
    private static PassengerCouponService passengerCouponService;


    /**
     * 基本费用
     */
    public static double getOrderFare(OrderEvaluateParam orderEvaluateParam, SysWayCalculationDto sysWayCalculationDto) {
        Integer adultNum = orderEvaluateParam.getAdultNum();
        Integer typeJoin = orderEvaluateParam.getTypeJoin();
        //包车
        if (typeJoin == CommonConstant.ORDER_TYPE_JOIN_CHARTERED) {
            return sysWayCalculationDto.getPriceWeep();
        }
        // 拼车
        if (typeJoin == CommonConstant.ORDER_TYPE_JOIN_ORDINARY) {
            double unit = sysWayCalculationDto.getPriceUnit();
            //成人
            return adultNum * unit;
        }
        // 拼车
        if (typeJoin == CommonConstant.ORDER_TYPE_JOIN_TWO_PERSONS) {
            double twoPersonPrice = sysWayCalculationDto.getPriceTwoPerson();
            //成人
            return adultNum * twoPersonPrice;
        }
        return 0d;
    }

    /**
     * 围栏费用
     */
    public static FenceRaisePriceVo getFenceFare(OrderEvaluateParam orderEvaluateParam, String sysWayUuid) {
        FenceRaisePriceParam fenceRaisePriceParam = new FenceRaisePriceParam();
        BeanUtils.copyProperties(orderEvaluateParam, fenceRaisePriceParam);
        fenceRaisePriceParam.setWayUuid(sysWayUuid);
        ResponseData<FenceRaisePriceVo> fenceInfoRsd = sysWayFenceService.queryFenceRaisePrice(fenceRaisePriceParam);
        if (!fenceInfoRsd.isSuccess() || fenceInfoRsd.getData() == null) {
            log.info("线路:{}未获取到围栏加价信息,{},参数{}", sysWayUuid, fenceInfoRsd.getMsg(), JsonUtils.toJSONString(fenceRaisePriceParam));
            return new FenceRaisePriceVo();
        }
        return fenceInfoRsd.getData();
    }

    /**
     * 夜间费用
     */
    public static Double getNightFare(OrderEvaluateParam orderEvaluateParam, String sysWayUuid) {
        Map<String, Object> paramMap = new HashMap<>();
        //获取夜间加价信息
        paramMap.clear();
        paramMap.put("wayUuid", sysWayUuid);
        paramMap.put("deparTime", orderEvaluateParam.getDeparTime());
        List<SysWayNightFareDto> nightFareList = sysWayNightFareService.list(paramMap).getData();
        if (CollectionUtils.isEmpty(nightFareList)) {
            log.info("线路:{}未获取到夜间加价信息,参数{}", sysWayUuid, paramMap);
            return 0d;
        } else {
            return nightFareList.get(0).getRaisePrice();
        }
    }

    /**
     * 计算优惠券
     */
    public static ApiJoinValuationVo calcuCouponFare(OrderEvaluateParam orderEvaluateParam, ApiJoinValuationVo joinValuationVo) {
        //实际价格
        double totalFare = joinValuationVo.getTotalFare();
        double actPaid = totalFare;
        //优惠券
        if (StringUtils.isNotEmpty(orderEvaluateParam.getPassengerUuid())) {
            SysPointSiteDto systemPointSiteDto = sysPointSiteService.findOne(MapUtils.build("uuid", orderEvaluateParam.getOriginUuid())).getData();
            if (systemPointSiteDto != null) {
                String originCity = systemPointSiteDto.getCityName();
                String couponUuid;
                double couponFare;
                List<PassengerCouponDto> passengerCouponList = passengerCouponService.findJoinMinCoupon(orderEvaluateParam.getPassengerUuid(),
                        CommonConstant.BUSINESS_TYPE_POOL, totalFare, originCity).getData();
                if (CollectionUtils.isNotEmpty(passengerCouponList)) {
                    PassengerCouponDto couponDto = passengerCouponList.get(0);
                    couponUuid = couponDto.getUuid();
                    couponFare = couponDto.getMoney();
                    actPaid = totalFare - couponFare;
                    if (actPaid < 0) {
                        actPaid = 0;
                    }
                    couponFare = EstimatedFareHelper.getDoubleValue(couponFare, 2);
                    if (couponFare > 0d) {
                        joinValuationVo.setCouponMoney(couponFare);
                        joinValuationVo.setCouponUuid(couponUuid);
                    }
                }
            }
        }
//        joinValuationVo.setActPaid(EstimatedFareHelper.getDoubleValue(actPaid, 1));
        joinValuationVo.setActPaid(EstimatedFareHelper.getDoubleValue(actPaid, 2));
        return joinValuationVo;
    }

    /**
     * 获取估价信息
     */
    public static ApiJoinValuationVo buildFareItem(OrderEvaluateParam orderEvaluateParam, SysWayCalculationDto sysWayCalculationDto, ApiJoinValuationVo joinValuationVo) {
        int adultNum = orderEvaluateParam.getAdultNum();
        List<OrderCostItemVo> list = new ArrayList<>();
        int sort = 1;
        //拼&包车费用计算
        double totalFare = 0;
        //包车
        if (orderEvaluateParam.getTypeJoin() == CommonConstant.ORDER_TYPE_JOIN_CHARTERED) {
            double weep = sysWayCalculationDto.getPriceWeep();
            totalFare = weep;
//            list.add(new OrderCostItemVo("包车费", String.valueOf(EstimatedFareHelper.getDoubleValue(totalFare, 1)), sort++, true));
            list.add(new OrderCostItemVo("包车费", String.valueOf(EstimatedFareHelper.getDoubleValue(totalFare, 2)), sort++, true));
        }
        // 拼车
        if (orderEvaluateParam.getTypeJoin() == CommonConstant.ORDER_TYPE_JOIN_ORDINARY) {
            double unit = sysWayCalculationDto.getPriceUnit();
            //成人
            totalFare += adultNum * unit;
//            list.add(new OrderCostItemVo("拼车费", String.valueOf(EstimatedFareHelper.getDoubleValue(totalFare, 1)), sort++, true));
            list.add(new OrderCostItemVo("拼车费", String.valueOf(EstimatedFareHelper.getDoubleValue(totalFare, 2)), sort++, true));
        }
        // 拼车
        if (orderEvaluateParam.getTypeJoin() == CommonConstant.ORDER_TYPE_JOIN_TWO_PERSONS) {
            double twoPersonPrice = sysWayCalculationDto.getPriceTwoPerson();
            //成人
            totalFare += adultNum * twoPersonPrice;
//            list.add(new OrderCostItemVo("二人快车", String.valueOf(EstimatedFareHelper.getDoubleValue(totalFare, 1)), sort++, true));
            list.add(new OrderCostItemVo("二人快车", String.valueOf(EstimatedFareHelper.getDoubleValue(totalFare, 2)), sort++, true));
        }
        //获取围栏加价信息
        double originFenceRaisePrice = joinValuationVo.getOriginRaisePrice();
        double destFenceRaisePrice = joinValuationVo.getDestRaisePrice();
        //起点围栏加价
        if (originFenceRaisePrice > 0) {
            list.add(new OrderCostItemVo("起点特殊区域加价", String.valueOf(originFenceRaisePrice), sort++, true));
        }
        //终点围栏加价
        if (destFenceRaisePrice > 0) {
            list.add(new OrderCostItemVo("终点特殊区域加价", String.valueOf(destFenceRaisePrice), sort++, true));
        }
        double nightRaisePrice = joinValuationVo.getNightRaisePrice();
        //夜间加价
        if (nightRaisePrice > 0) {
            list.add(new OrderCostItemVo("夜间加价", String.valueOf(nightRaisePrice), sort++, true));
        }

        //优惠券
        if (joinValuationVo.getCouponMoney() != null && joinValuationVo.getCouponMoney() > 0) {
//            list.add(new OrderCostItemVo("优惠券", "-" + EstimatedFareHelper.getDoubleValue(joinValuationVo.getCouponMoney(), 1), sort++, true));
            list.add(new OrderCostItemVo("优惠券", "-" + EstimatedFareHelper.getDoubleValue(joinValuationVo.getCouponMoney(), 2), sort++, true));
        }
        // 价格明细
        joinValuationVo.setCostItems(list);

        return joinValuationVo;
    }

    @Reference
    public void setSysWayFenceService(SysWayFenceService sysWayFenceService) {
        CrosstownEvaluateHelper.sysWayFenceService = sysWayFenceService;
    }

    @Reference
    public void setSysWayNightFareService(SysWayNightFareService sysWayNightFareService) {
        CrosstownEvaluateHelper.sysWayNightFareService = sysWayNightFareService;
    }

    @Reference
    public void setSysWayCalculationService(SysWayCalculationService sysWayCalculationService) {
        CrosstownEvaluateHelper.sysWayCalculationService = sysWayCalculationService;
    }

    @Reference
    public void setSysPointSiteService(SysPointSiteService sysPointSiteService) {
        CrosstownEvaluateHelper.sysPointSiteService = sysPointSiteService;
    }

    @Reference
    public void setPassengerCouponService(PassengerCouponService passengerCouponService) {
        CrosstownEvaluateHelper.passengerCouponService = passengerCouponService;
    }

}
